
//
//  WYCTRepayCell.m
//  信用卡管理
//
//  Created by ZX on 2017/11/14.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCTRepayCell.h"

@implementation WYCTRepayCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    WYCTRepayCell * cell = [tableView dequeueReusableCellWithIdentifier:[WYCTRepayCell ID]];
    //    cell.backgroundColor = [UIColor redColor];
    cell = [[WYCTRepayCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[WYCTRepayCell ID]];
    cell.backgroundColor = WYCGlobalBg;
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setMember];
        
    }
    return self;
}
- (void)setMember{
    
    /** 顶部View */
    self.topView = [[UIView alloc] init];
    self.topView.frame = CGRectMake(0, 0, ScreenWidth, 160 * px);
    self.topView.backgroundColor = WYCGlobalBg;
    [self.contentView addSubview:self.topView];
    
    
    
    /** label*/
    self.banklabel = [[UILabel alloc] init];
    self.banklabel.font = [UIFont systemFontOfSize:50 * px];
    self.banklabel.frame = CGRectMake(40 * px, 60 * px, 260 * px, 80 * px);
    self.banklabel.textColor = [UIColor blackColor];
    [self.topView addSubview:self.banklabel];
    
    
    
    
    /** 输入框*/
    self.repayTextField = [[MyTextField alloc] init];
    self.repayTextField.frame = CGRectMake(CGRectGetMaxX(self.banklabel.frame), self.banklabel.y, _topView.width , 80 * px);
//    self.repayTextField.backgroundColor = [UIColor redColor];
    self.repayTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.repayTextField.textColor = [UIColor blackColor];
    self.repayTextField.font = [UIFont systemFontOfSize:50 * px];
     [self.repayTextField addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.topView addSubview:self.repayTextField];
    
    
    
    
    /** lable*/
    self.lsnameberlabel = [[UILabel alloc] init];
    self.lsnameberlabel.frame = CGRectMake(CGRectGetMaxX(self.banklabel.frame) + 80 * px, self.banklabel.y, _topView.width / 2, self.banklabel.height);
    self.lsnameberlabel.textColor = WYCRGBColor(186, 186, 186);
    self.lsnameberlabel.font = forLight;
    [self.topView addSubview:self.lsnameberlabel];
    
    
    //+号按钮
    self.addButton = [[UIButton alloc] init];
    self.addButton.frame = CGRectMake(self.topView.width - 290 * px, 40 * px, 80 * px, 80 * px);
    [self.addButton setImage:[UIImage imageNamed:@"icon_count_zj"] forState:UIControlStateNormal];
    [self.topView addSubview:self.addButton];
    
    
    //-号按钮
    self.rustButton = [[UIButton alloc] init];
    self.rustButton.frame = CGRectMake(CGRectGetMaxX(self.addButton.frame) + 40 * px, 40 * px, self.addButton.width, self.addButton.width);
    [self.rustButton setImage:[UIImage imageNamed:@"icon_count_js"] forState:UIControlStateNormal];
    [self.topView addSubview:self.rustButton];
    
    
    //日历
    self.riliButton = [[UIButton alloc] init];
    self.riliButton .frame = CGRectMake(self.rustButton.x, 40 * px, self.addButton.width, self.addButton.width);
//    [self.riliButton  setImage:[UIImage imageNamed:@"Calculator"] forState:UIControlStateNormal];
//    [self.topView addSubview:self.riliButton ];
    
}
- (void)valueChanged:(UITextField *)TexField{
    
    self.repaymodel.coment = TexField.text;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TextField" object:nil];
}
+(NSString *)ID{
    return @"cell";
}
- (void)setRepaymodel:(WYCRepayModel *)repaymodel{
    _repaymodel = repaymodel;
    if (repaymodel.isenland != YES) {
        self.repayTextField.enabled = YES;
    }else{
        
        self.repayTextField.enabled = NO;
    }
    
    self.banklabel.text = repaymodel.title;
    
    [self.repaymodel addObserver:self forKeyPath:@"coment" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    
    if (![repaymodel.title isEqualToString:@"还款天数："]) {
        self.addButton.hidden = YES;
        self.rustButton.hidden = YES;
        
    }else{
        self.addButton.hidden = NO;
        self.rustButton.hidden = NO;
        self.lsnameberlabel.text = repaymodel.coment;
    }
    
    if (![repaymodel.title isEqualToString:@"消费日期:"]) {
        self.riliButton.hidden = YES;
    }else{
        self.riliButton.hidden = NO;
    }
    
    
    
    self.repayTextField.placeholder = repaymodel.titleFeild;
    self.repayTextField.text = repaymodel.coment;
    
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    self.repayTextField.text = self.repaymodel.coment;
}

- (void)dealloc {
    
    [self.repaymodel removeObserver:self forKeyPath:@"coment"];
}

@end
